package com.easyoffer.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easyoffer.common.BaseResponse;
import com.easyoffer.common.DeleteRequest;
import com.easyoffer.common.util.BlackIpUtil;
import com.easyoffer.common.util.NetUtils;
import com.easyoffer.constant.UserConstant;
import com.easyoffer.exception.BusinessException;
import com.easyoffer.model.dto.question.*;
import com.easyoffer.model.entity.Question;
import com.easyoffer.model.entity.User;
import com.easyoffer.model.vo.QuestionBankVO;
import com.easyoffer.service.QuestionService;
import com.easyoffer.common.ErrorCode;
import com.easyoffer.common.util.ResultUtils;
import com.easyoffer.model.vo.QuestionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/question")
public class QuestionController {
    @Resource
    QuestionService questionService;
    /**
     * 查询题目内容
     * @param id 题目id
     * @return 题目详情
     */
    @GetMapping("/get")
    public BaseResponse<QuestionVO> getQuestion(@RequestParam(required = false) Long id,HttpServletRequest request){
        if (id == null)
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        QuestionVO questionVO = questionService.getQuestion(id,request);
        return ResultUtils.success(questionVO);
    }



    @GetMapping("/list")
    public BaseResponse<Page<QuestionVO>> listQuestionByPage(QuestionQueryRequest queryRequest, HttpServletRequest request){
        if (queryRequest == null)
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(questionService.listQuestionByPage(queryRequest));
    }

    @GetMapping("/search")
    public BaseResponse<Page<QuestionVO>> searchQuestion(QuestionQueryRequest queryRequest,HttpServletRequest request){
        if (queryRequest == null)
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        Page<QuestionVO> questionVOPage = questionService.searchQuestionFromEs(queryRequest,request);
        return ResultUtils.success(questionVOPage);
    }

    /**
     * 添加题目
     * @param questionAddRequest 题目添加请求体
     * @param request HttpServletRequest
     * @return 添加成功后返回id值，-1则失败
     */
    @PostMapping("/add")
    @SaCheckRole("admin")
    public BaseResponse<Long> addQuestion(@RequestBody QuestionAddRequst questionAddRequest, HttpServletRequest request){
        if (questionAddRequest == null)
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        return questionService.addQuestion(questionAddRequest, request);
    }



    @PostMapping("/update")
    @SaCheckRole("admin")
    public BaseResponse<Integer> updateQuestion(@RequestBody QuestionUpdateRequest updateRequest, HttpServletRequest request){
        if (updateRequest == null)
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        return questionService.updateQuestion(updateRequest, request);
    }

    @PostMapping("/delete")
    @SaCheckRole("admin")
    public BaseResponse<Boolean> deleteQuestion(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request){
        if (deleteRequest == null)
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        return questionService.deleteQuestion(deleteRequest, request);
    }


    @PostMapping("/batch/delete")
    @SaCheckRole("admin")
    public BaseResponse<Boolean> batchDeleteQuestions(@RequestBody List<Long> questionIdList){
        if (questionIdList == null)
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        questionService.batchDeleteQuestions(questionIdList);
        return ResultUtils.success(true);
    }


}
